/**
 * @(#)com.framework.web.wechat.controller.WeChatGoodsController
 *
 * Copyright (c) 2014-2018 一抹风光
 *
 * DESC:
 *
 */
package com.framework.web.wechat.controller;

import com.framework.core.constant.Globals;
import com.framework.core.domain.SessionVo;
import com.framework.web.bus.domain.Commentinfo;
import com.framework.web.bus.domain.SystemConfig;
import com.framework.web.bus.service.CommentinfoService;
import com.framework.web.bus.service.SystemConfigService;
import com.framework.web.common.domain.BusinessConstants;
import com.framework.web.sys.domain.User;
import com.framework.web.sys.service.UserService;
import com.framework.web.wechat.domain.WeChatLoginInfo;
import com.framework.web.wechat.domain.WeChatRequest;
import com.framework.web.wechat.domain.WeChatResInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 系统配置相关功能
 *
 * @author fusf
 * @version 1.0  2016/9/11
 */
@Controller
@RequestMapping("/weChat/system")
public class WeChatSystemController {

    @Autowired
    private SystemConfigService systemConfigService;

    @Autowired
    private CommentinfoService commentinfoService;

    @Autowired
    private UserService userService;


    /**
     * 反馈意见
     *
     * @param requestInfo
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/feedBack", method = RequestMethod.GET)
    @ResponseBody
    public Object feedBack(WeChatRequest requestInfo, HttpServletRequest request) throws Exception {
        WeChatResInfo resInfo = new WeChatResInfo();
        ServletContext application = request.getSession().getServletContext();
        if (application.getAttribute(requestInfo.getUserGuid()) == null) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("status",BusinessConstants.WECHAT_STATUS.E50010003);
            map.put("msg","登陆权限已经过期");
            resInfo.setResult(map);
            return resInfo;
        }
        SessionVo sessionVo = (SessionVo) application.getAttribute(requestInfo.getUserGuid());
        Commentinfo commentinfo = new Commentinfo();
        commentinfo.setOpinion(requestInfo.getAdvice());
        commentinfo.setCreateBy(sessionVo.getUserId());
        commentinfo.setCreateName(sessionVo.getUserName());
        commentinfo.setIsRead(BusinessConstants.IS_READ.NO);//未读
        commentinfo.setCreateTime(new Date());
        commentinfoService.save(commentinfo);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status",BusinessConstants.WECHAT_STATUS.SUCCESS);
        map.put("msg", "成功");
        resInfo.setResult(map);
        return resInfo;

    }


    /**
     * 关于我们
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/aboutInfo", method = RequestMethod.GET)
    @ResponseBody
    public Object aboutInfo(HttpServletRequest request) throws Exception {
        WeChatResInfo resInfo = new WeChatResInfo();
        SystemConfig systemConfig = systemConfigService.findAll();
        if (systemConfig == null) {
            return resInfo;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("title", systemConfig.getTitle()); //标题
        map.put("email", systemConfig.getEmail());//邮箱
        map.put("description", systemConfig.getDescription());  //正宗的阳澄湖大闸蟹
        map.put("phone", systemConfig.getServiceTel());  //客服电话
        map.put("qq", systemConfig.getServiceQQ());  //客服qq
        map.put("qqGroup", systemConfig.getServiceQQGrop());  //客服qq群
        map.put("scanImage", ""); //我们的微信二维码  TODO
        resInfo.setResult(map);
        return resInfo;

    }


    /**
     * 用户注册或登录
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/loginWeChar", method = RequestMethod.GET)
    @ResponseBody
    public Object loginWeChar(WeChatLoginInfo loginInfo, HttpServletRequest request) throws Exception {
        WeChatResInfo resInfo = new WeChatResInfo();
        List<User> userList = userService.findByOpenId(loginInfo.getOpenid());
        User user = new User();
        if (userList != null && userList.size() > 0) {
            user = userList.get(0);
        } else {
            //新增
            String sex = "fe";
            if ("0".equals(loginInfo.getSex())) {
                sex = "ma";
            }
            user.setSex(sex);
            user.setCreateTime(new Date());
            user.setNickName(loginInfo.getNickname());
            user.setOpenId(loginInfo.getOpenid());
            user.setImage(loginInfo.getHeadimgurl());
            user.setIntegration(0);//积分
            user.setUserType(0);
            //新增加的参数
            user.setLanguage(loginInfo.getLanguage());
            user.setCity(loginInfo.getCity());
            user.setProvince(loginInfo.getProvince());
            user.setCountry(loginInfo.getCountry());
            user.setCountry(loginInfo.getCountry());
            user.setSubscribeTime(new Date());
            user.setStatus(Globals.STATUS_YES);
            userService.save(user);
        }
        SessionVo sessionVo = new SessionVo();
        sessionVo.setUserId(user.getId());
        sessionVo.setUserName(user.getNickName());
        ServletContext application = request.getSession().getServletContext();
        String userGuid = UUID.randomUUID().toString();
        application.setAttribute(userGuid, sessionVo);
        application.setAttribute(user.getOpenId(), userGuid);
        Map<String, Object> map = new HashMap<>();
        map.put("status", BusinessConstants.WECHAT_STATUS.SUCCESS); //标志位
        map.put("msg", "成功");//消息
        map.put("userGuid", userGuid);//UUID
        resInfo.setResult(map);
        return resInfo;

    }


}
